var box = document.getElementsByClassName("box")[0];
var ynpassword_left = document.getElementsByClassName('ynpassword_left')[0];
var ynpassword_left_ok = document.getElementsByClassName('ynpassword_left_ok')[0];
var password_ok_show = false;
if (localStorage.length !== 0) {//保存记住密码勾选状态
    if (localStorage.getItem('password_ok_show') === 'true') {
        password_ok_show = true;
    } else {
        password_ok_show = false;
    }
}
if (password_ok_show === true) {
    ynpassword_left_ok.style.color = 'white';
} else if (password_ok_show === false) {
    ynpassword_left_ok.style.color = '#4484D5';
}
if (password_ok_show === false || password_ok_show === 'false') {//记住密码勾选状态决定是否清楚缓存
    localStorage.clear();
}
$.post(Feng.ctxPath + "/outpatientManagement/getList", function (data) {
    var txt = '';
    for (var i = 0; i < data.length; i++) {
        txt += '<option value="' + data[i].simplename + '">' + data[i].simplename + '</option>';
    }
    document.getElementById('outpatientName').innerHTML = txt;
    var outpatientName = document.getElementById('outpatientName').value;
    if (outpatientName != "") {
        $("#outpatientName option[value='" + outpatientName + "']").attr("selected", "selected");
    }
    var optionALL = $("#outpatientName").find("option");
    for (var q = 0; q < optionALL.length; q++) {
        if (localStorage.getItem('outpatientName') === optionALL[q].innerHTML) {
            optionALL[q].selected = true;
        }
    }
}, "json");

$('#userName').val(localStorage.getItem('userName'));//页面刷新保存用户登陆账号信息
$('#passWord').val(localStorage.getItem('passWord'));//页面刷新保存用户登陆密码信息
box.style.width = document.body.clientWidth + "px";//获取视口宽。设置最外层容器宽
box.style.height = document.body.clientHeight + "px";//获取视口高。设置最外层容器高

ynpassword_left.onclick = function () {//记住密码
    password_ok_show = !password_ok_show;
    if (password_ok_show === true) {
        ynpassword_left_ok.style.color = 'white';
    } else if (password_ok_show === false) {
        ynpassword_left_ok.style.color = '#4484D5';
    }
};

$(document).on('keypress', function (event) {
    if (event.keyCode == 13) {
        var userName = $('#userName').val();
        var passWord = $('#passWord').val();
        var outpatientName = $('#outpatientName').val();
        localStorage.setItem('userName', userName);
        localStorage.setItem('passWord', passWord);
        localStorage.setItem('outpatientName', outpatientName);
        localStorage.setItem('password_ok_show', password_ok_show);
        $.ajax({
            url: Feng.ctxPath + "/login",
            type: "post",
            data: {
                "username": userName,
                "outpatientName": outpatientName,
                "password": passWord
            },
            success: function (data) {
                window.location.href = Feng.ctxPath + "/index";
            },
            error: function (data) {
                alert('账号和密码不匹配，请联系管理员!');
                window.location.href = Feng.ctxPath + "/login";
            }
        });
    }
});

$('#submit_login').click(function () {
    var userName = $('#userName').val();
    var passWord = $('#passWord').val();
    var outpatientName = $('#outpatientName').val();
    localStorage.setItem('userName', userName);
    localStorage.setItem('passWord', passWord);
    localStorage.setItem('outpatientName', outpatientName);
    localStorage.setItem('password_ok_show', password_ok_show);
    $.ajax({
        url: Feng.ctxPath + "/login",
        type: "post",
        data: {
            "username": userName,
            "outpatientName": outpatientName,
            "password": passWord
        },
        success: function (data) {
            window.location.href = Feng.ctxPath + "/index";
        },
        error: function (data) {
            alert('账号和密码不匹配，请联系管理员!');
            window.location.href = Feng.ctxPath + "/login";
        }
    });
});

cleanTips = function () {
    $("#tips").html("");
};

$(function () {
    $("#tips").html("<h4 style=\"color: red;\">" + document.getElementById("tipsValue").value + "</h4>");
    $("#kaptcha").on('click', function () {
        $("#kaptcha").attr('src', Feng.ctxPath + '/kaptcha?' + Math.floor(Math.random() * 100)).fadeIn();
    });
});